﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DauGiaApp.Models;
namespace DauGiaApp.Controllers
{
    public class LoaiSanPhamController : Controller
    {
        //
        // GET: /LoaiSanPham/
        DauGiaEntities data = new DauGiaEntities();
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult DanhSach()
        {
            return PartialView(data.LoaiSanPhams.ToList());
        }

        [HttpPost]
        public ActionResult Them(LoaiSanPham lsp)
        {
            try
            {
                data.LoaiSanPhams.AddObject(lsp);
                data.SaveChanges();
                return Content("successful");
            }
            catch (Exception ex)
            {
                return Content("fail");
            }
                    
        }

        [HttpPost]
        public ActionResult CapNhat(LoaiSanPham lsp)
        {
            try
            {
                LoaiSanPham lsp_old = data.LoaiSanPhams
                                .ToList()
                                .Where(tmp => tmp.MaLoaiSanPham == lsp.MaLoaiSanPham)
                                .Single();

                lsp_old.TenLoaiSanPham = lsp.TenLoaiSanPham;
                data.SaveChanges();
                return Content("successful");
            }
            catch
            {
                return Content("fail");
            }            
        }

        [HttpPost]
        public ActionResult Xoa(LoaiSanPham lsp)
        {
            int MaLoaiSanPham = lsp.MaLoaiSanPham;
            try
            {
                 LoaiSanPham spXoa = data.LoaiSanPhams
                                    .ToList()
                                    .Where(tmp => tmp.MaLoaiSanPham == MaLoaiSanPham)
                                    .Single();
                if (spXoa != null)
                {
                    data.LoaiSanPhams.DeleteObject(spXoa);
                    data.SaveChanges();
                }
                return Content("successful");
            }
            catch(Exception ex)
            {
                return Content("fail");
            }
           
            
        }


    }
}
